package DataAccess;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.sun.net.ssl.internal.ssl.Provider;

import pojo.DevicePOJO;

public class DeviceDA extends BaseDA {
	public DevicePOJO get(String regID){
		String sql="select * from device where regId='"+regID+"'";
		DevicePOJO rs=null;
		provider.open();
		
		try {
			ResultSet rst=provider.executeQuery(sql);
			rs=new DevicePOJO();
			while(rst.next()){
				rs.setId(rst.getInt("ID"));
				rs.setRegID(rst.getString("regID"));
				rs.setTenDangNhap(rst.getString("tenDangNhap"));
			}
		} catch (SQLException e) {
			rs=null;
			e.printStackTrace();
		}
		provider.close();
		return rs;
	}
	public ArrayList<DevicePOJO>get(){
		String sql="select * from device";
		ArrayList<DevicePOJO> rs=null;
		provider.open();
		
		try {
			ResultSet rst=provider.executeQuery(sql);
			rs=new ArrayList<DevicePOJO>();
			while(rst.next()){
				DevicePOJO dv=new DevicePOJO();
				dv.setId(rst.getInt("ID"));
				dv.setRegID(rst.getString("regID"));
				dv.setTenDangNhap(rst.getString("tenDangNhap"));
				rs.add(dv);
			}
		} catch (SQLException e) {
			rs=null;
			e.printStackTrace();
		}
		provider.close();
		return rs;
	}
	public boolean insert(DevicePOJO device){
		String sql="INSERT INTO `device` (`regID`, `tenDangNhap`) VALUES ('"+device.getRegID()+"', '"+device.getTenDangNhap()+"')";
		provider.open();
		int rs=provider.executeUpdate(sql);
		provider.close();
		return rs==-1?false:true;
	}
	@Override
	protected Object getProperties(ResultSet rss) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}
	
}
